const baseUrl = uni.baseURL

function convertObj(data) {
	var _result = [];
	for (var key in data) {
		var value = data[key];
		if (value.constructor == Array) {
			value.forEach(function(_value) {
				_result.push(key + "=" + _value);
			});
		} else {
			_result.push(key + '=' + value);
		}
	}
	return _result.join('&');
}

const http = (config) => {
	let url = config.url,
		data = (config.data || config.params)
	// let hideLoading = option.hideLoading || false // 是否显示 loading
	// let hideMsg = option.hideMsg || false // 是否隐藏错误提示
	// let token = '' // 登录鉴权获得的 token
	// uni.getStorage({
	//     key: 'token',
	//     success: (ress) => {
	//         token = ress.data
	//     }
	// })
	// if (!hideLoading) {
	//     uni.showLoading({
	//         title: '加载中...',
	//         mask: true
	//     })
	// }
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: config.method || 'POST', // 默认 post 请求
			header: {
				token: uni.getStorageSync('token')
			},
			data: data,
			success: res => { // 服务器成功返回的回调函数
				if (uni.versiontype) {
					let obj = JSON.parse(uni.getStorageSync('versiontype'))
					setTimeout(() => {
						uni.showModal({
							title: '提示',
							content: '发现新版本' + obj.version + '！',
							showCancel: false,
							success: function(ress) {
								if (ress.confirm) {
									plus.runtime.openURL(obj.fullPath,
										function(res) {

										});
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}, 1000)
				}
				if (res.statusText == 'request:fail') {
					uni.showToast({
						title: '网络异常',
						duration: 2000,
						icon: 'none'
					});
				}
				if (res.data.code == 401) {
					let routes = getCurrentPages()
					let curRoute = routes[routes.length - 1]
					let Url = undefined;
					// console.log(curRoute.route)
					// console.log(123123)
					if (curRoute) {
						// #ifdef H5
						Url = curRoute.route + '?pageLinkType=' + (curRoute.$vm.LinkType ||
							'') + '&' + convertObj(curRoute
								.options)
						// #endif
						// #ifndef H5
						Url = curRoute.route + '?pageLinkType=' + (curRoute.$vm.LinkType ||
							'') + '&' + convertObj(curRoute
								.options)
						// #endif
					}
					if (curRoute.route == 'pages/login/login') {
						uni.reLaunch({
							url: '/pages/login/login?invitacode=' + (curRoute.options
								.invitacode || '')
						});
						return res.data
					}
					if (Url) {
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login?Url=' +
									encodeURIComponent(Url) + '&invitacode=' +
									(curRoute.options.invitacode || '')
							});
						}, 100)
					} else {
						uni.reLaunch({
							url: '/pages/login/login?invitacode=' + (curRoute.options
								.invitacode || '')
						});
					}

				}
				resolve(res.data)
			},
			fail: (err) => { // 接口调用失败的回调函数
				uni.showToast({
					title: '网络异常',
					duration: 2000,
					icon: 'none'
				});
				reject(error)
			}
		})
	})
}
export default http
